﻿@* Generator: Template TypeVisibility: Internal GeneratePrettyNames: true *@
@using System
@using System.Collections.Generic
@using Hangfire
@using Hangfire.Dashboard
@using Hangfire.Dashboard.Pages
@using Hangfire.Dashboard.Resources
@using Hangfire.States
@using Hangfire.Storage
@inherits RazorPage
@{
    Layout = new LayoutPage(Strings.AwaitingJobsPage_Title);

    int from, perPage;

    int.TryParse(Query("from"), out from);
    int.TryParse(Query("count"), out perPage);

    List<string> jobIds = null;
    Pager pager = null;

    using (var connection = Storage.GetReadOnlyConnection())
    {
        var storageConnection = connection as JobStorageConnection;

        if (storageConnection != null)
        {
            pager = new Pager(from, perPage, storageConnection.GetSetCount("awaiting"));
            jobIds = storageConnection.GetRangeFromSet("awaiting", pager.FromRecord, pager.FromRecord + pager.RecordsPerPage - 1);
        }
    }
}

<div class="row">
    <div class="col-md-3">
        @Html.JobsSidebar()
    </div>
    <div class="col-md-9">
        <h1 class="page-header">@Strings.AwaitingJobsPage_Title</h1>

        @if (jobIds == null)
        {
            <div class="alert alert-warning">
                <h4>@Strings.AwaitingJobsPage_ContinuationsWarning_Title</h4>
                <p>@Strings.AwaitingJobsPage_ContinuationsWarning_Text</p>
            </div>
        }
        else if (jobIds.Count > 0)
        {
            <div class="js-jobs-list">
                <div class="btn-toolbar btn-toolbar-top">
                    @if (!IsReadOnly)
                    {
                        <button class="js-jobs-list-command btn btn-sm btn-primary"
                                data-url="@Url.To("/jobs/awaiting/enqueue")"
                                data-loading-text="@Strings.Common_Enqueueing">
                            <span class="glyphicon glyphicon-repeat"></span>
                            @Strings.Common_EnqueueButton_Text
                        </button>
                    }
                    @if (!IsReadOnly)
                    {
                        <button class="js-jobs-list-command btn btn-sm btn-default"
                                data-url="@Url.To("/jobs/awaiting/delete")"
                                data-loading-text="@Strings.Common_Deleting"
                                data-confirm="@Strings.Common_DeleteConfirm">
                            <span class="glyphicon glyphicon-remove"></span>
                            @Strings.Common_DeleteSelected
                        </button>
                    }
                    @Html.PerPageSelector(pager)
                </div>

                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                @if (!IsReadOnly)
                                {
                                    <th class="min-width">
                                        <input type="checkbox" class="js-jobs-list-select-all"/>
                                    </th>
                                }
                                <th class="min-width">@Strings.Common_Id</th>
                                <th>@Strings.Common_Job</th>
                                <th class="min-width">@Strings.AwaitingJobsPage_Table_Options</th>
                                <th class="min-width">@Strings.AwaitingJobsPage_Table_Parent</th>
                                <th class="align-right">@Strings.Common_Created</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var jobId in jobIds)
                            {
                                JobData jobData;
                                StateData stateData;
                                StateData parentStateData = null;

                                using (var connection = Storage.GetReadOnlyConnection())
                                {
                                    jobData = connection.GetJobData(jobId);
                                    stateData = connection.GetStateData(jobId);

                                    if (stateData != null && stateData.Name == AwaitingState.StateName)
                                    {
                                        parentStateData = connection.GetStateData(stateData.Data["ParentId"]);
                                    }
                                }

                                <tr class="js-jobs-list-row @(jobData != null ? "hover" : null)">
                                    @if (!IsReadOnly)
                                    {
                                        <td>
                                            <input type="checkbox" class="js-jobs-list-checkbox" name="jobs[]" value="@jobId"/>
                                        </td>
                                    }
                                    <td class="min-width">
                                        @Html.JobIdLink(jobId)
                                    </td>
                                    @if (jobData == null)
                                    {
                                        <td colspan="2"><em>@Strings.Common_JobExpired</em></td>
                                    }
                                    else
                                    {
                                        <td class="word-break">
                                            @Html.JobNameLink(jobId, jobData.Job)
                                        </td>
                                        <td class="min-width">
                                            @if (stateData != null && stateData.Data.ContainsKey("Options") && !String.IsNullOrWhiteSpace(stateData.Data["Options"]))
                                            {
                                                var optionsDescription = stateData.Data["Options"];
                                                if (Enum.TryParse(optionsDescription, out JobContinuationOptions options))
                                                {
                                                    optionsDescription = options.ToString("G");
                                                }
                                                <code>@optionsDescription</code>
                                            }
                                            else
                                            {
                                                <em>@Strings.Common_NotAvailable</em>
                                            }
                                        </td>
                                        <td class="min-width">
                                            @if (parentStateData != null)
                                            {
                                                <a href="@Url.JobDetails(stateData.Data["ParentId"])" class="text-decoration-none">
                                                    @Html.StateLabel(parentStateData.Name, parentStateData.Name, hover: true)
                                                </a>
                                            }
                                            else
                                            {
                                                <em>@Strings.Common_NotAvailable</em>
                                            }
                                        </td>
                                        <td class="min-width align-right">
                                            @Html.RelativeTime(jobData.CreatedAt)
                                        </td>
                                    }
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
                @Html.Paginator(pager)
            </div>
        }
        else
        {
            <div class="alert alert-info">
                @Strings.AwaitingJobsPage_NoJobs
            </div>
        }
    </div>
</div>
